package com.minggo.charmword.util;

import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.minggo.charmword.dao.DBConfig;
import com.minggo.charmword.dao.DaoUtils;
import com.minggo.charmword.dao.DbOpenHelper;
import com.minggo.charmword.model.Plan;

/**
 * 计划工具类
 * @author minggo
 * @date 2014-10-22 下午4:33:44
 */
public class PlanUtil {
	
	/**
	 * 保存计划
	 * 
	 * @param context
	 * @param user
	 */
	public static void savePlan(Context context, Plan plan) {
		try {
			
			SQLiteDatabase db = new DbOpenHelper(context).getWritableDatabase();
			db.replace(DBConfig.TABLE_PLAN, null, DaoUtils.object2ContentValues(plan));
			db.close();
		} catch (Exception e) {
			e.printStackTrace();
			return;
		}
	}
	
	/**
	 * 获取计划
	 * 
	 * @param context
	 * @return
	 */
	public static Plan getPlan(Context context) {
		List<Plan> planList = null;
		try {
			SQLiteDatabase db = new DbOpenHelper(context).getReadableDatabase();
			Cursor query = db.query(DBConfig.TABLE_PLAN, null, null, null, null, null, null);
			planList = DaoUtils.cursor2ObjectList(query, Plan.class);
			db.close();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		if (planList!=null&&!planList.isEmpty()) {
			return planList.get(0);
		}
		return null;
	}
}
